import { getIP, setIP } from "../../dao/common"

const state = {
  ip: 'http://10.156.8.224',
  navigation: ''
}

const getters = {
  ip: state => state.ip,
  navigation: state => state.navigation
}

const mutations = {
  SET_IP(state, ip) {
    state.ip = ip
  },
  SET_NAVIGATION(state, nav) {
    state.navigation = nav
  }
}

const actions = {
  loadIP({ commit }) {
    return new Promise((resolve, reject) => {
      getIP().then(val => {
        if (val) {
          resolve(commit('SET_IP', val))
        } else {
          reject('unset')
        }
      }).catch(e => {
        reject(e)
      })
    })
  },
  setIP({ commit }, ip) {
    return new Promise((resolve, reject) => {
      setIP(ip).then(() => {
        resolve(commit('SET_IP', ip))
      }).catch(e => {
        reject(e)
      })
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
